﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'P_UpdateEnterprise')
BEGIN
	DROP  Procedure  [esf_sso].P_UpdateEnterprise
END
GO


CREATE Procedure [esf_sso].P_UpdateEnterprise
	@pEnterpriseID					INT = NULL OUT
	
	,@pIdentificationNumber			VARCHAR (30)
	,@pIdentificationTypeCode		VARCHAR (60) = 'NIT'
	,@pIdentificationNumberDigit	VARCHAR (10) = NULL
	
	,@pCode							VARCHAR (50) = NULL
	,@pName							VARCHAR (50) = NULL
	,@pDescription					VARCHAR (1000) = NULL
	,@pIsEnabled					BIT = 1
	,@pURL							VARCHAR (500) = NULL
	
	,@pUserLastUpdateAlias	VARCHAR (20) = 'root'
	,@pLastUpdateDateTime	DATETIME = NULL
AS

	IF @pLastUpdateDateTime IS NULL
	BEGIN
		SET @pLastUpdateDateTime = GETDATE ()
	END


	UPDATE 
		[esf_sso].[BasicReference] 
	SET
		[Code] = @pCode,
		[Name] = @pName,
		[Description] = @pDescription,
		[IsEnabled] = @pIsEnabled,
		[LastUpdateDateTime] = @pLastUpdateDateTime,
		UserLastUpdateAlias = @pUserLastUpdateAlias,
		Extra_I = @pURL
	WHERE
		ID = @pEnterpriseID


	UPDATE [esf_sso].[Enterprise]
    SET
       [IdentificationNumber] = @pIdentificationNumber
       ,[IdentificationTypeID] = (SELECT ID FROM esf_sso.VIdentificationType WHERE Code = @pIdentificationTypeCode)
       ,[IdentificationNumberDigit] = @pIdentificationNumberDigit
    WHERE
       [EnterpriseID] = @pEnterpriseID
           
GO